const Extenditure = require("../../../utils/Http/Extenditure")
// pages/home/home.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        extenditure: [],
        income: [],
        show: false,
        day: "今天",
        currentDate: new Date().getTime(),
        minDate: new Date().getTime() - 315360000000,
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
    },
    onInput(event) {
        this.setData({
            currentDate: event.detail,
        });
    },
    showPopup() {
        this.setData({
            show: true
        });
    },

    onClose() {
        this.setData({
            show: false
        });
    },
    selectItem(event) {
        console.log(event.currentTarget.dataset);
        var {
            aid
        } = event.currentTarget.dataset
        var extenditure = this.data.extenditure;
        var income = this.data.income;
        extenditure.forEach(item => {
            if (item._id == aid) {
                item.checked = true;
            } else {
                item.checked = false;
            }
        })
        income.forEach(item => {
            if (item._id == aid) {
                item.checked = true;
            } else {
                item.checked = false;
            }
        })
        this.setData({
            extenditure,
            income
        })
        // wx.request({
        //     url: `http://127.0.0.1:7001/selectIcon?id=${aid}`,
        // })
    },
    handleNum(num) {
        if (num < 10) {
            num = "0" + num;
        }
        return num
    },
    checkedTime() {
        var time = this.data.currentDate;
        console.log(time);
        var month = this.handleNum(new Date(time).getMonth() + 1);
        var day = this.handleNum(new Date(time).getDate());
        this.setData({
            day: month + "-" + day
        })
        this.onClose()
    },
    formsubmit(event) {
        console.log(event.detail.value);
        var {
            money,
            remarks
        } = event.detail.value;
        var date = this.data.currentDate;
        var extenditure = this.data.extenditure;
        var income = this.data.income;
        var name;
        var type;
        var imgUrl;
        extenditure.forEach(item => {
            if (item.checked) {
                name = item.name;
                type = "extenditure";
                imgUrl = item.imgUrl
            }
        })
        income.forEach(item => {
            if (item.checked) {
                if (item.name == "其他") {
                    name = "其他i";
                } else {
                    name = item.name;
                }
                type = "income";
                imgUrl = item.imgUrl;
            }
        })
        if (type == "extenditure") {
            money = -money
        }
        wx.request({
            url: 'http://127.0.0.1:7001/add',
            method: "POST",
            data: {
                username: "lyx",
                name,
                imgUrl,
                money,
                remarks,
                date
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded' //post
            },
            success(res) {
                console.log(res.data)
            },
            fail(err) {
                console.log(err);
            }
        })
        event.detail.value = ""
        wx.switchTab({
          url: '../home',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        Extenditure.getExtenditureHttp().then(res => {
            // console.log(res.data);
            this.setData({
                extenditure: res.data
            })
        });
        Extenditure.getIncomeHttp().then(res => {
            res.data.forEach(item => {
                if (item.name == "其他i") {
                    item.name = "其他"
                }
            })
            this.setData({
                income: res.data
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})